package com.iutr.shared.utils;

import java.util.Optional;

import com.iutr.shared.model.Result;
import com.iutr.shared.model.ResultStatus;
import com.iutr.shared.model.ServiceResult;

/**
 * 接口响应工具类
 *
 * @author lss
 * @date 2020/8/21, 周五
 */
public class ResultUtils {

    /**
     * quick return a success model
     *
     * @return {@link Result}
     */
    public static <D> Result<D> newSuccessfulResult() {
        return newSuccessfulResult(null);
    }

    /**
     * quick return a success model
     *
     * @param data data
     * @return {@link Result}
     */
    public static <D> Result<D> newSuccessfulResult(D data) {
        return newSuccessfulResult(null, data);
    }


    /**
     * quick return a success model
     *
     * @param data data
     * @return {@link Result}
     */
    public static <D> Result<D> newSuccessfulResult(String message, D data) {
        Result<D> r = new Result<>();
        r.setSuccess(true);
        r.setCode(ResultStatus.SUCCESS.getCode());
        r.setMessage(message);
        Optional.ofNullable(data).ifPresent(r::setData);
        return r;
    }

    /**
     * quick return a failed model
     *
     * @param msg error message
     * @return {@link }
     */
    public static <D> Result<D> newFailedResult(String msg, int code) {
        return newFailedResult(msg, code, null);
    }

    public static <D> Result<D> newFailedResult(String msg) {
        return newFailedResult(msg, ResultStatus.UN_HANDLE_ERROR.getCode(), null);
    }

    public static <D> Result<D> newFailedResult(ResultStatus status) {
        return newFailedResult(status.getMessage(), status.getCode());
    }

    /**
     * quick return a failed model
     *
     * @param msg error message
     * @return {@link Result}
     */
    public static <D> Result<D> newFailedResult(String msg, D data) {
        return newFailedResult(msg, ResultStatus.SUCCESS.getCode(), data);
    }

    /**
     * quick return a failed model
     *
     * @param msg error message
     * @return {@link Result}
     */
    public static <D> Result<D> newFailedResult(String msg, int code, D data) {
        Result<D> r = new Result<>();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage(msg);
        Optional.ofNullable(data).ifPresent(r::setData);
        return r;
    }

    /**
     * 返回一个成功的service result
     *
     * @return {@link ServiceResult}
     */
    public static <D> ServiceResult<D> newSuccessfulServiceResult() {
        ServiceResult<D> sr = new ServiceResult<>();
        sr.setSuccess(true);
        sr.setCode(ResultStatus.SUCCESS.getCode());
        return sr;
    }

    /**
     * 调用外部接口错误返回的服务层结果
     *
     * @param message 错误信息
     * @return {@link ServiceResult}
     */
    public static <D> ServiceResult<D> callOuterApiFailedServiceResult(ServiceResult<D> r, String message) {
        if (r == null) {
            r = new ServiceResult<>();
        }
        r.setSuccess(false);
        r.setCode(ResultStatus.THIRD_PARTY_CALL_ERROR.getCode());
        r.setMessage(message);
        return r;
    }

    /**
     * 修改r为成功模型， 不会修改outerResult
     *
     * @param r 结果模型，如果为空则新建
     * @param message 错误消息
     * @param <D> 泛型
     * @return 传入的模型（传入为空则新建）
     */
    public static <D> ServiceResult<D> callOuterApiSuccessServiceResult(ServiceResult<D> r, String message) {
        if (r == null) {
            r = new ServiceResult<>();
        }
        r.setSuccess(true);
        r.setCode(ResultStatus.SUCCESS.getCode());
        Optional.ofNullable(message).ifPresent(r::setMessage);
        return r;
    }

    /**
     * 修改r为成功模型，并设置数据
     *
     * @param r 结果模型，如果为空则新建
     * @param data 数据泛型
     * @return 传入的模型
     */
    public static <D> ServiceResult<D> callOuterApiSuccessServiceResult(ServiceResult<D> r, D data) {
        if (r == null) {
            r = new ServiceResult<>();
        }
        r.setSuccess(true);
        r.setCode(ResultStatus.SUCCESS.getCode());
        r.setData(data);
        return r;
    }
}
